import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../controller/tabbar_controller.dart';

class TabbarPage extends GetView<TabbarController> {
  TabbarPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<TabbarController>(
      builder: (_) => Scaffold(
        backgroundColor: Colors.white,
        body: IndexedStack(
          index: controller.tabIndex.value,
          children: controller.tabPageList,
        ),
        bottomNavigationBar: _buildBottomTab(),
      ),
    );
  }

  /// 底部tab
  Widget _buildBottomTab() {
    return BottomNavigationBar(
      unselectedItemColor: const Color(0xFF666666),
      selectedItemColor: const Color(0xFF000000),
      selectedFontSize: 12.0,
      unselectedFontSize: 10.0,
      type: BottomNavigationBarType.fixed,
      items: [
        BottomNavigationBarItem(
          icon: Padding(
            padding: EdgeInsets.only(top: 12.h, bottom: 6.h),
            child: Icon(
              Icons.home,
              color:
                  controller.tabIndex.value == 0 ? Colors.black : Colors.grey,
            ),
          ),
          label: '首页',
        ),
        BottomNavigationBarItem(
          icon: Padding(
            padding: EdgeInsets.only(top: 12.h, bottom: 6.h),
            child: Icon(
              Icons.circle,
              color:
                  controller.tabIndex.value == 1 ? Colors.black : Colors.grey,
            ),
          ),
          label: 'action',
        ),
        BottomNavigationBarItem(
          icon: Padding(
            padding: EdgeInsets.only(top: 12.h, bottom: 6.h),
            child: Icon(
              Icons.circle,
              color:
                  controller.tabIndex.value == 2 ? Colors.black : Colors.grey,
            ),
          ),
          label: '我的',
        ),
      ],
      currentIndex: controller.tabIndex.value,
      onTap: (int index) async {
        if (controller.tabIndex.value == index) {
          return;
        }
        controller.tabIndex.value = index;
        controller.update();
      },
    );
  }
}
